/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchForm;

import exceptions.NoResultsException;
import ui.messages.MessageBoxes;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Class listens to changes on progress bar and calls finish
 * search when progress reaches maximum.
 *
 * @author rymek
 *         User: rymek
 *         Date: 01.08.11
 *         Time: 12:24
 */
class SearchFormChangeListener implements ChangeListener {

    private final SearchForm searchForm;        // parent Search Form

    /**
     * Constructor.
     * Sets reference to parent object
     *
     * @param searchForm parent object
     */
    public SearchFormChangeListener(SearchForm searchForm) {
        this.searchForm = searchForm;
    }

    /**
     * Checks if search form should be reset to new search state after search is finished
     *
     * @param e the event
     */
    @Override
    public void stateChanged(ChangeEvent e) {

        if (this.searchForm.getSearchProcessProgress().getValue() == this.searchForm.getSearchProcessProgress().getMaximum()) {

            try {
                this.searchForm.finishSearch();

            } catch (NoResultsException error) {
                MessageBoxes.noResultsFoundException(error);
            }
        }
    }
}
